/*
* 构件核验结果查询
* */

import 'dart:convert';

import 'package:center_beijing/api/product_api.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/common/utils.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:center_beijing/widgets/imageSelect_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart' as Go;
import 'package:dio/dio.dart' as ImageForm;
import 'package:images_picker/images_picker.dart';
import 'dart:io';

class QualityElementDetail extends StatefulWidget {
  @override
  _QualityElementDetailState createState() => _QualityElementDetailState();
}

class _QualityElementDetailState extends State<QualityElementDetail> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  var waterId = Go.Get.arguments;
  var workDetail = Go.Get.arguments;
  var workFlow = [];
  List<Image> imgs = [];
  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.setState(() {
      workFlow = json.decode(workDetail['formJson']);
      if(workDetail['elementSpotCheckAttaList']!=null){
        var image = workDetail['elementSpotCheckAttaList'].map<Image>((item){
          return Image.network(
            "${GlobalConfig.baseUrl}fs/irest/file/mps_fileTransferHandler/image?id=${item['attachmentId']}&ZJQFSESSION=${GlobalConfig.session}",
            fit: BoxFit.fill,
          );
        }).toList();
        imgs = image;
      }
    });
  }

  /*
  * 获取详情
  * */
  getWorkStageDetail() async {
    var result = await getCompleteWorkStageDetail(waterId);
    if (result != null) {
      var image = result['attachments'].map<Image>((item){
        return Image.network(
          "${GlobalConfig.baseUrl}fs/irest/file/mps_fileTransferHandler/image?id=${item['attachmentId']}&ZJQFSESSION=${GlobalConfig.session}",
          fit: BoxFit.fill,
        );
      }).toList();
      this.setState(() {
        workDetail = result;
        workFlow = result['formJson']!=null?json.decode(result['formJson']):[];
        imgs = image;
      });
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('核验详情'),
        ),
        body: Container(
          color: Styles.MainBackgroundColor,
          padding: EdgeInsets.fromLTRB(
              0, 0, 0, MediaQuery.of(context).padding.bottom + 70),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: EdgeInsets.fromLTRB(Styles.spacing8, Styles.spacing8,
                      Styles.spacing8, Styles.spacing8),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: EdgeInsets.fromLTRB(Styles.spacing8,
                            Styles.spacing8, Styles.spacing8, Styles.spacing8),
                        color: Colors.white,
                        margin: EdgeInsets.fromLTRB(
                            0, Styles.spacing8, 0, Styles.spacing8),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              '检查项',
                              style: TextStyle(
                                  color: Styles.DetailColor,
                                  fontSize: Styles.Middle16Font),
                            ),
                            Column(
                              children: workFlow.map((item) {
                                return Container(
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        '检查项目: ${item['title']}',
                                        style: TextStyle(
                                            color: Styles.LeftTitleColor,
                                            fontSize: Styles.NormalFont13),
                                      ),
                                      Text(
                                        '检查结果: ${item['valueStr'] != null ? item['valueStr'] : '--'}',
                                        style: TextStyle(
                                            color: Styles.LeftTitleColor,
                                            fontSize: Styles.NormalFont13),
                                      ),
                                    ],
                                  ),
                                );
                              }).toList(),
                            ),
                          ],
                        ),
                      ),
                      imgs.length>0?Container(
                        color: Colors.white,
                        margin: EdgeInsets.fromLTRB(
                            0, Styles.spacing8, 0, Styles.spacing8),
                        height: 260.h,
                        child: Swiper(
                          itemBuilder: (BuildContext context, int index) {
                            //条目构建函数传入了index,根据index索引到特定图片
                            return imgs[index];
                          },
                          itemCount: imgs.length,
                          autoplay: false,
                          pagination: new SwiperPagination(), //这些都是控件默认写好的,直接用
                        ),
                      ):Container(),
                      Container(
                        padding: EdgeInsets.fromLTRB(Styles.spacing8, Styles.spacing8,
                            Styles.spacing16, Styles.spacing8),
                        child: Text(
                          '备注',
                          style: TextStyle(
                              color: Styles.DetailColor, fontSize: Styles.Middle16Font),
                        ),
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        margin: EdgeInsets.fromLTRB(
                            Styles.spacing8, Styles.spacing8, 0, Styles.spacing8),
                        child: Text(
                          workDetail['remark'] != null ? workDetail['remark'] : '--',
                          style: TextStyle(
                              fontSize: Styles.NormalFont,
                              color: Styles.DetailColor),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
    );
  }
}
